package com.mach.platform.api.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * Nginx图片代理工具类
 */
public class ImageUtil {

    /**
     * 设备机组图片和背景图片显示Linux地址(其他路径)，使用nginx代理
     */
    private static final String otherLinuxShowPath = "https://www.gjwll.com/image/applets/other/";

    /**
     * 头像显示地址Linux路径，使用nginx代理
     */
    private static final String avatarLinuxShowPath = "https://www.gjwll.com/image/applets/avatar/";

    /**
     * 头像图片保存linux路径
     */
    private static final String avatarLinuxSavePath = "/home/workshop/image/applets/avatar/";

    /**
     * 设备机组图片和背景图片显示和存放Windows地址(其他路径)
     */
    private static final String otherWinPath = "C:\\Users\\Administrator\\Desktop\\image\\applets\\other\\";

    /**
     * 头像显示和保存Windows路径
     */
    private static final String avatarWinPath = "C:\\Users\\Administrator\\Desktop\\image\\applets\\avatar\\";

    /**
     * 消息图片Linux显示路径
     */
    private static final String messageLinuxShowPath = "https://www.gjwll.com/image/applets/message/";

    /**
     * 消息图片Linux保存路径
     */
    private static final String messageLinuxSavePath = "/home/workshop/image/applets/message/";

    /**
     * 消息图片windows保存和显示路径
     */
    private static final String messageWinPath = "C:\\Users\\Administrator\\Desktop\\image\\applets\\message\\";


    /**
     * 当前操作系统的名称
     */
    private static String osName;

    /**
     * 初始化信息
     */
    static {
        osName = System.getProperty("os.name").toLowerCase();
    }

    /**
     * 设备名称和设备类型的映射关系
     * @param devName 设备名称
     * @return
     */
    public static String getDevType(String devName) {
        String devType = "";
        if (devName.contains("冷干机")) {
            devType = "1";
        }
        if (devName.contains("干燥机")) {
            devType = "2";
        }
        if (devName.contains("冷却塔")) {
            devType = "3";
        }
        if (devName.contains("离心机")) {
            devType = "4";
        }
        if (devName.contains("螺杆机")) {
            devType = "5";
        }
        if (devName.contains("制冷主机")) {
            devType = "6";
        }
        if (devName.contains("冷却泵") || devName.contains("冷冻泵")) {
            devType = "7";
        }
        return devType;
    }


    /**
     * 根据运行状态，获取对应的图片颜色
     * @param runStatus 运行状态
     * @return
     */
    public static String getImageColour(int runStatus) {
        String colour = "";
        if (runStatus == 0) {
            colour = "gray";
        }
        if (runStatus == 1) {
            colour = "green";
        }
        if (runStatus == 2) {
            colour = "red";
        }
        if (runStatus == 3) {
            colour = "yellow";
        }
        return colour;
    }


    /**
     * 通过设备组名称获取设备机组图片路径
     * @param devGroupName 设备机组名称
     * @return
     */
    public static String getImagePath(String devGroupName) {
        String devType = getDevType(devGroupName);
        String path = otherLinuxShowPath;
        if (osName.startsWith("win")) {
            path = otherWinPath;
        }
        // 设备组是蓝色的机组图片
        return path + devType + "_blue.png";
    }


    /**
     * 根据设备名称和运行状态获取设备图标路径和背景图片路径
     * @param devName 设备名称
     * @param runStatus 运行状态
     * @return
     */
    public static Map getImagePath(String devName, int runStatus) {
        String devType = getDevType(devName);
        String colour = getImageColour(runStatus);
        String path = otherLinuxShowPath;
        if (osName.startsWith("win")) {
            path = otherWinPath;
        }
        String devImagePath = path + devType + "_" + colour + ".png";
        String backgroundPath = path + colour + ".png";
        Map result = new HashMap();
        result.put("devImagePath", devImagePath);
        result.put("backgroundPath", backgroundPath);
        return result;
    }


    /**
     * 通过openId获取头像文件
     * @param openId 用户openId
     * @return
     */
    public static File getAvatarImage(String openId) {
        String path = avatarLinuxSavePath;
        if (osName.startsWith("win")) {
            path = avatarWinPath;
        }
        return new File(path + openId + ".jpg");
    }


    /**
     * 把网络地址的图片保存到本地
     * @param openId 用户openId
     * @param avatarUrl 头像网络URL
     * @return
     */
    public static boolean uploadUrlAvatar(String openId, String avatarUrl) {
        boolean flag = true; // 记录保存图片是否成功
        InputStream is = null;
        OutputStream os = null;
        try {
            URL url = new URL(avatarUrl);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(4000); // 设置连接的超时时间
            is = con.getInputStream();
            byte[] bs = new byte[1024];
            int length;
            os = new FileOutputStream(getAvatarSavePath() + openId + ".jpg");
            // 开始读取图片信息
            while ((length = is.read(bs)) != -1) {
                os.write(bs, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        } finally {
            try {
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }


    /**
     * 获取消息图片保存路径
     * @param type 路径类型 save：保存路径 show：展示路径
     * @return
     */
    public static String getMessageImagePath(String type) {
        String path = (type.equals("save") ? messageLinuxSavePath : messageLinuxShowPath);
        if (osName.startsWith("win")) {
            path = messageWinPath;
        }
        return path;
    }


    /**
     * 上传用户头像到服务端
     * @param openId 用户openId
     * @param file 头像图片
     */
    public static boolean uploadAvatar(String openId, MultipartFile file) {
        File image = getAvatarImage(openId);
        try {
            file.transferTo(image);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 获取头像保存地址
     * @return
     */
    public static String getAvatarSavePath() {
        String path = avatarLinuxSavePath;
        if (osName.startsWith("win")) {
            path = avatarWinPath;
        }
        return path;
    }


    /**
     * 通过用户的openId获取对应的头像显示地址
     * @param openId 用户的openId
     * @return
     */
    public static String getAvatarShowPath(String openId) {
        String path = avatarLinuxShowPath;
        if (osName.startsWith("win")) {
            path = avatarWinPath;
        }
        return path + openId + ".jpg";
    }


}
